﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;
using System.Xml.Linq;
using ToodleDo;
using Toodledo.Model;
using Toodledo.Model.API;


namespace Toodledo.Client
{
	public partial class Session : IAuthentication
	{
        #region Implementation of IAuthentication

        string IAuthentication.GetToken(string userid, string appid, string apptoken)
        {
            //set token for debug
            //TokenStore.TokenValue = "td4f082b895028d";
           
            //load the token first                
            string token = TokenStore.TokenValue;
            //TimeSpan lastupdate = DateTime.Now - TokenStore.LastUpdate;
            if (_isRevalidating||userid != TokenStore.UserId)//||token == null || lastupdate.TotalHours > 4)
            {
                string sig = MD5(userid + apptoken);
                string query =
                    FormatArgument(userid, "userid") +
                    FormatArgument(appid, "appid") +
                    FormatArgument(sig, "sig");
                //var parameters = String.Format("userid={0}{1}", userid, (String.IsNullOrEmpty(appid) ? String.Empty : ";appid=" + appid));
                token = ReadElement<String>(SendRequest(ActionTypes.GetToken, query));                
                Debug.WriteLine(token);
                //save it
                TokenStore.UserId = userid;
                TokenStore.TokenValue = token;
                Logger.InvokeMessageEvent("Getting new Token", Logger.MessageType.Tracking);
            }                       
            //return it
            return token;
        }
      
        string IAuthentication.GetUserId(string email, string password, string appid, string apptoken)
        {
            return GetUserId(email, password,appid, apptoken);
        }

        Account IAuthentication.GetAccountInfo()
        {
            var results = SendRequest(ActionTypes.GetAccountInfo);
            Account account = new Account
                                  {
                                      Alias = ReadElement<string>(results, "alias"),
                                      DateFormat = (DateFormat) ReadElement<int>(results, "dateformat"),
                                      HideMonths = ReadElement<int>(results, "hidemonths"),
                                      HotListDueDate = ReadElement<int>(results, "hotlistduedate"),
                                      HotListPriority = (Priority) ReadElement<int>(results, "hotlistpriority"),
                                      IsPro = (ReadElement<int>(results, "pro") != 0),
                                      TimeZone = ReadElement<int>(results, "timezone"),
                                      UserId = ReadElement<string>(results, "userid"),
                                      LastFolderEdit = ReadElement(results, "lastedit_folder"),
                                      LastTaskEdit = ReadElement(results, "lastedit_task"),
                                      LastTaskDelete = ReadElement(results, "lastdelete_task")                                  
                              };
            Logger.InvokeMessageEvent("Account requested", Logger.MessageType.Tracking);         
            return account;
        }
              

        #endregion Implementation of IAuthentication
	}
}
